<template>
  <div class="loading-container">
    <inline-loading></inline-loading>
    <p class="loading-txt">正在加载</p>
  </div>
</template>

<script>
  import { getUserInfo, getCallback } from '@/api/login'
  import { InlineLoading } from 'vux'

  export default {
    components: {InlineLoading},
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    },
//    beforeRouteEnter (to, from, next) {
//      console.log('to: ', to)
//      console.log('from: ', from)
//      if (process.env.NODE_ENV === 'development') {
        // return next({name: 'Home'})
//    beforeRouteEnter (to, from, next) {
//      console.log('to: ', to)
//      console.log('from: ', from)
//      if (store.getters.isLogin) {
//        next({name: 'Home'})
//      } else {
//        let code = to.query.code
//        console.log('preview code: ', code)
//        // 登录
//        if (!code) {
//          store.dispatch('userSignin')
//          next(false)
//        } else {
//          console.log('code: ', code)
//          getCallback({code: code, state: to.query.state}).then(
//            response => {
//              console.log('data: ', response.data)
//              store.commit('updateUserInfo', response.data)
//              return next({name: 'Home'})
//            }
//          )
//        }
//      }
//    },
    created () {
      console.log('query: ', this.$route.query.code)
      console.log('params: ', this.$route.params)
      if (this.$store.getters.isLogin) {
        this.$router.push({name: 'Home'})
      } else {
        let code = this.$route.query.code
        console.log('code: ', code)
        // 登录
        if (!code) {
          this.$store.dispatch('userSignin')
//          next(false)
        } else {
          getCallback({code: code, state: this.$route.query.state}).then(
            response => {
              console.log('data: ', response.data)
              this.$store.commit('updateUserInfo', response.data)
              this.$router.push({name: 'Home'})
            }
          )
        }
      }
    },
    methods: {
      loginBy () {
        getUserInfo().then(response => {
          this.$store.commit('updateUserInfo', response.data.user_info)
          this.$router.push({ path: '/home' })
        })
      }
    }
  }
</script>
<style scoped>
  .loading-container {
    height: 100vh;
    text-align: center;
    padding-top: 50%;
  }
  .weui-loading {
    width: 80px;
    height: 80px;
  }
  .loading-txt {
    font-size: 18px;
    color: #666;
  }
</style>

